package com.xtedu.examproject.scheduler;

import com.xtedu.examproject.scheduler.job.ExamStatusJob;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;

public class QuartzScheduler {
    private static Scheduler scheduler;

    public static void start() throws SchedulerException {
        scheduler = StdSchedulerFactory.getDefaultScheduler();

        JobDetail job = JobBuilder.newJob(ExamStatusJob.class)
                .withIdentity("examStatusJob", "examGroup")
                .build();

        Trigger trigger = TriggerBuilder.newTrigger()
                .withIdentity("examStatusTrigger", "examGroup")
                .startNow()
                .withSchedule(SimpleScheduleBuilder.simpleSchedule()
                        .withIntervalInMinutes(1)
                        .repeatForever())
                .build();

        scheduler.scheduleJob(job, trigger);
        scheduler.start();
        System.out.println("考试状态定时任务已启动");
    }

    public static void shutdown() throws SchedulerException {
        if (scheduler != null) {
            scheduler.shutdown();
            System.out.println("考试状态定时任务已关闭");
        }
    }
}
